/**
 * @(#) WelfareDetailProxy.as
 *
 * @date		2009. 03. 19.
 * @version		1.0
 * @author		Shin, Yunwoo
 * @description	선택적 복리후생 상세데이터 조회Proxy - Proxy
 * 				선택적 복리후생 상세데이터 조회
 */
package com.skt.common_hr.welfare.model
{
	import com.skt.common_hr.common.puremvc.CommonProxy;
	import com.skt.common_hr.common.puremvc.DataReceiver;
	import com.skt.common_hr.welfare.WelfareFacade;
	import com.skt.common_hr.welfare.model.vo.BankAccountVO;
	import com.skt.common_hr.welfare.model.vo.ProfileVO;
	import com.skt.common_hr.welfare.model.vo.ReqWelfareVO;
	import com.skt.common_hr.welfare.model.vo.UsedAmountVO;
	import com.skt.common_hr.welfare.model.vo.WelfareDetailVO;
	import com.wcl.dataloader.*;
	import com.wcl.utils.DateUtil;
	import com.wcl.utils.NumberUtil;
	
	import mx.collections.ArrayCollection;
	import mx.controls.Alert;
	import mx.resources.ResourceManager;
	import mx.rpc.events.FaultEvent;
	import mx.rpc.events.ResultEvent;
		
	public class WelfareDetailProxy extends CommonProxy
	{
		public static const NAME:String = "WelfareDetailProxy";
		
		public var detailList:ArrayCollection;
		public var detailMaster:WelfareDetailVO;
		public var defaultBank:BankAccountVO = null;
		public var additionBank:BankAccountVO = null;
		public var msgStr:String = "";
		
		public var printList:ArrayCollection;
		public var printMaster:WelfareDetailVO;
		public var printUsedAmount:UsedAmountVO;
		public var profile:ProfileVO;
		
//		public var dataProvider:WelfareDetailVO;
//		public var key:String;
		
		
		//프린트추가(총금액)
		public var totMoney:Number = 0;
		
		/**
		 * 생성자
		 * 
		 * @param
		 * @return void
		 */
		public function WelfareDetailProxy(data:Object=null)
		{
			super(NAME, data);
		}
		
		
		/**
		 * data load를 수행
		 * 		- DataLoader 를 생성하고 Import Parameter 객체를 생성, RFC를 호출
		 * 		- 선복내역 LIST 조회
		 * @param pernr 사번
		 * 		  Gkey  사용항목 key값
		 * @return void
		 */
		public function load(key:String):void
		{
			
			var param:Object = new Object();
			param["I_GWAREKEY"] 	= key;
			param["I_ACTIO"] 		= '2';
			loadMultiData("Z_HR_0060", param, result); 
		}
		
		/*****
		 * 신청
		 * ***/
		public function insert(pernr:String, year:String, table1:Array, table2:Array):void
		{
			var param:Object = new Object();
			param["I_ACTIO"] 		= '3';
			param["I_PERNR"] 		= pernr;
			param["I_GJAHR"] 		= year;
			var tables:Object  = new Object();
			tables["OTAB1"] = table1;
			tables["OTAB2"] = table2;
			
			saveMultiData("Z_HR_0060", param, tables, resultInsert); 
		}
		
		/****
		 * 변경
		 * ***/
		public function update(pernr:String, year:String, table1:Array, table2:Array, Gkey:String):void
		{
			
			var param:Object = new Object();
			param["I_ACTIO"] 		= '4';
			param["I_GWAREKEY"] 	= Gkey;
			param["I_PERNR"] 		= pernr;
			param["I_GJAHR"] 		= year;
			var tables:Object  = new Object();
			tables["OTAB1"] = table1;
			tables["OTAB2"] = table2;
			
			saveMultiData("Z_HR_0060", param, tables, resultUpdate); 
		}
		
		/****
		 * 삭제
		 * ***/
		public function deleteData(pernr:String, Gkey:String):void
		{
			var params:Object = new Object();
			params["I_GWAREKEY"] = Gkey;
			params["I_ACTIO"] = "5";
			
			loadMultiData("Z_HR_0060", params, deleteResult);
			
		}
		
		/**
		 * data load를 수행
		 * 		- DataLoader 를 생성하고 Import Parameter 객체를 생성, RFC를 호출
		 * 		- 기타계좌 콤보박스 조회 
		 * @param 
		 * @return void
		 */
		public function accountLoad(pernr:String):void
		{
			var param:Object = new Object();
			param["I_PERNR"] = pernr;
			loadMultiData("Z_HR_0042", param, accountResult);
		}
		
		/**
		 * 프린트 상세정보
		 * */
		public function printDetailData(pernr:String, key:String):void
		{
			var param:Object = new Object();
			param["I_PERNR"] 		= pernr;
			param["I_GWAREKEY"] 	= key;
			param["I_ACTIO"] 		= '2';
			loadMultiData("Z_HR_0060", param, printDetailResult); 
		}
		
		/**
		 * 프린트금액
		 * */
		public function printMoney(pernr:String, year:String):void
		{
			var param:Object = new Object();
			param["I_PERNR"] 		= pernr;
			param["I_GJAHR"]		= year;
			loadMultiData("Z_HR_0059", param, printMoneyResult); 
		}
		
		
		/**
		 * 개인정보
		 * */
		public function printUserInfoMoney(pernr:String):void
		{
			var param:Object = new Object();
			param["I_PERNR"] = pernr;
			loadMultiData("Z_HR_0008", param, printUserInfoResult); 
		}
		
		/**
		 * guideSubject
		 * */
		public function guideSubject():void
		{
			var param:Object = new Object();
			loadMultiData("Z_HR_0056", param, guideSubjectResult); 
		}
		
		/**
		 * guideDetail
		 * */
		public function guideDetail():void
		{
			var param:Object = new Object();
			loadMultiData("Z_HR_0057", param, guideDetailResult); 
		}				
		

		
		
		///////////////////////////////////////////////////////////////////////////////////////////////////
		//
		//	Event Listeners
		//
		///////////////////////////////////////////////////////////////////////////////////////////////////
		/**
		 * 서버에서 정상적으로 리턴된 결과를 처리
		 * 
		 * @param
		 * @return void
		 */
		public function result(dr:DataReceiver):void
		{
			if(dr.retCode==0) {  
				detailList = parseWelfareDetailList(dr.result.OTAB1);
				detailMaster = parseWelfareDetailMaster(dr.result.OTAB2);
				facade.sendNotification(WelfareFacade.LOAD_WELFARE_DETAIL_COMPLETE, this);
			}
			else {
				facade.sendNotification(WelfareFacade.LOAD_WELFARE_DETAIL_FAILED, this);
			} 
		}
		
		
		
		/**
		 * 서버에서 정상적으로 리턴된 결과를 처리
		 * 
		 * @param
		 * @return void
		 */
		public var O_GWAREKEY:String;
		public function resultInsert(dr:DataReceiver):void
		{
			
			if(dr.retCode==0) {
				detailList = parseWelfareDetailList(dr.result.OTAB1);
				detailMaster = parseWelfareDetailMaster(dr.result.OTAB2);
				O_GWAREKEY = dr.result.O_GWAREKEY.toString();
				facade.sendNotification(WelfareFacade.SAVE_WELFARE_COMPLETE, this);
			}
			else {
				trace( "INSERT failed to load date list: (" +dr.result["RETCODE"] + ") "+dr.result["RETTEXT"]);
				msgStr = dr.result["RETTEXT"];
				facade.sendNotification(WelfareFacade.SAVE_WELFARE_FAILED, this);
			} 
		}
		
		
		/**
		 * 서버에서 정상적으로 리턴된 결과를 처리
		 * 
		 * @param
		 * @return void
		 */
		public function resultUpdate(dr:DataReceiver):void
		{
			
			if(dr.retCode==0) {
				detailList = parseWelfareDetailList(dr.result.OTAB1);		//신청현황
				detailMaster = parseWelfareDetailMaster(dr.result.OTAB2);	//계정정보 및 처리현황
				O_GWAREKEY = dr.result.O_GWAREKEY;
				facade.sendNotification(WelfareFacade.UPDATE_WELFARE_COMPLETE, this);
			}
			else {
				trace( "INSERT failed to load date list: (" +dr.result["RETCODE"] + ") "+dr.result["RETTEXT"]);
				msgStr = dr.result["RETTEXT"];
				facade.sendNotification(WelfareFacade.UPDATE_WELFARE_FAILED, this);
			} 
		}
		
		
		/**
		 * 서버에서 정상적으로 리턴된 결과를 처리
		 * 
		 * @param
		 * @return void
		 */
		public function deleteResult(dr:DataReceiver):void
		{
			
			if(dr.retCode==0) {  
				facade.sendNotification(WelfareFacade.DELETE_WELFARE_COMPLETE, this);
			}
			else {
				trace( "INSERT failed to load date list: (" +dr.result["RETCODE"] + ") "+dr.result["RETTEXT"]);
				msgStr = dr.result["RETTEXT"];
				facade.sendNotification(WelfareFacade.DELETE_WELFARE_FAILED, this);
			} 
		}
		
		
		/**
		 * 서버에서 정상적으로 리턴된 결과를 처리
		 * 계좌정보 조회
		 * @param
		 * @return void
		 */
		public function accountResult(dr:DataReceiver):void
		{
			if(dr.retCode==0) {  
				parseBankAccount(dr.result.BTAB);
				facade.sendNotification(WelfareFacade.RELOAD_BANK_ACCOUNT_COMPLETE, this);
			}
			else {
				trace( "failed to load date list: (" +dr.result["RETCODE"] + ") "+dr.result["RETTEXT"]);
				facade.sendNotification(WelfareFacade.RELOAD_BANK_ACCOUNT_COMPLETE, this);
			}
		}
		
		
		/**
		 * 프린트상세정보
		 * */
		public function printDetailResult(dr:DataReceiver):void
		{
			
			if(dr.retCode==0) {  
				printList = parseWelfareDetailList(dr.result.OTAB1);
				printMaster = parseWelfareDetailMaster(dr.result.OTAB2);
				facade.sendNotification(WelfareFacade.LOAD_PRINT_DETAIL_COMPLETE, this);
			}
			else {
				trace( "failed to load date list: (" +dr.result["RETCODE"] + ") "+dr.result["RETTEXT"]);
				msgStr = dr.result["RETTEXT"];
				facade.sendNotification(WelfareFacade.LOAD_WELFARE_DETAIL_FAILED, this);
			} 
		}
		
		
		/**
		 * 프린트금액
		 * */
		public function printMoneyResult(dr:DataReceiver):void
		{
			if(dr.retCode==0) {  
				printUsedAmount = parseUsedMoney(dr.result.OTAB1);		//상단사용금액
				//welfareList = parseWelfareList(event.result.OTAB2);		//mainList
				facade.sendNotification(WelfareFacade.LOAD_PRINT_MONEY_COMPLETE, this);
			}
		}
		
		
		/**
		 * 개인정보
		 * */
		public function printUserInfoResult(dr:DataReceiver):void
		{
			if(dr.retCode==0) {  
				profile = parseProfile(dr.result.RESULT[0]);
				facade.sendNotification(WelfareFacade.LOAD_PRINT_USERINFO_COMPLETE, this);
			}
			else {
				trace( "failed to load profile: (" +dr.result["RETCODE"] + ") "+dr.result["RETTEXT"]);
				facade.sendNotification(WelfareFacade.LOAD_PRINT_USERINFO_FAILED, this);
			}
		}
		
		/**
		 * 가이드 제목
		 * */
		public var iGetCnt:int=0;
		public var acGuideSubject:ArrayCollection;
		public function guideSubjectResult(dr:DataReceiver):void
		{
			if(dr.retCode==0) {  
				iGetCnt++;
				acGuideSubject = dr.result.OTAB;
				if(iGetCnt == 2) {
					iGetCnt = 0;
					facade.sendNotification(WelfareFacade.LOAD_GUIDE_COMPLETE, this);	
				}
			}
			else {
				trace( "failed to load guideSubjectResult: (" +dr.result["RETCODE"] + ") "+dr.result["RETTEXT"]);
			}
		}
				
		/**
		 * 가이드 상세
		 * */
		public var acGuideDetail:ArrayCollection;
		public function guideDetailResult(dr:DataReceiver):void
		{
			if(dr.retCode==0) {
				iGetCnt++  
				acGuideDetail = dr.result.OTAB;
				if(iGetCnt == 2) {
					iGetCnt = 0;
					facade.sendNotification(WelfareFacade.LOAD_GUIDE_COMPLETE, this);	
				}
				
			}
			else {
				trace( "failed to load guideDetailResult: (" +dr.result["RETCODE"] + ") "+dr.result["RETTEXT"]);
			}
		}
				
		// 가능한 신청일인지 체크 
		public function validReqDay(sTargetDate:String, sNowDate:String):void
		{
			var param:Object = new Object();
			param["I_WENDT"] = "03"; // 마감관리 구분 (03-선복,04-의료비)
			param["I_SYSDT"] = sNowDate; // 현재일
			param["I_IDATE"] = sTargetDate;	// 입력일

			loadData("Z_HR_0141", param, validReqDayResult, onFault); 
		}		

		private function onFault(event:FaultEvent):void
		{
			trace("Fault message in WelfareDetailProxy.as: " + event.fault.faultString);
		}		
		
		///////////////////////////////////////////////////////////////////////////////////////////////////
		//
		//	Data Parsing functions
		//
		///////////////////////////////////////////////////////////////////////////////////////////////////
		public function validReqDayResult(event:ResultEvent):void
		{
			if(event.result["RETCODE"]==0) {
				facade.sendNotification(WelfareFacade.LOAD_VALIDREQDAYRESULT_COMPLETE, this);
			}
			else {
				Alert.show("(" +event.result["RETCODE"] + ") "+event.result["RETTEXT"]);
				trace( "failed to load : (" +event.result["RETCODE"] + ") "+event.result["RETTEXT"]);
				//facade.sendNotification(MedicalMoneyFacade.LOAD_MM_LIMIT_DATA_FAILED, this);
			}
		}	
		
		/**
		 * 서버에서 전달된 result 객체를 분석하여 Value Object를 생성
		 * 
		 * @param
		 * @return 선택적복리후생 신청현황
		 */
		private function parseWelfareDetailList(srcArr:ArrayCollection):ArrayCollection
		{
			var retArr:ArrayCollection = new ArrayCollection();
			this.totMoney = 0;
			for each(var rec:Object in srcArr) 
			{
				var vo:ReqWelfareVO = new ReqWelfareVO();
				vo.PERNR 		= rec["PERNR"];
				vo.ENAME 		= rec["ENAME"];
				vo.IDATE 		= DateUtil.stringToDateTime(rec["IDATE"]);
				vo.UDATE 		= DateUtil.stringToDateTime(rec["UDATE"]);
				vo.SEQNO 		= rec["SEQNO"];
				vo.CAFGBN 		= rec["CAFGBN"];
				vo.CAFGTX 		= rec["CAFGTX"];
				vo.CAFTY 		= rec["CAFTY"];
				vo.CAFTX 		= rec["CAFTX"];
				vo.DTEXT 		= rec["DTEXT"];
				vo.BNFAMT 		= Math.floor(rec["BNFAMT"] * 10 * 10);
				
				vo.STGBN		= rec["STGBN"];
				vo.STGBN_TX		= rec["STGBN_TX"];
				vo.STCD2		= rec["STCD2"];
				vo.COMNM		= rec["COMNM"];
				vo.APGBN		= rec["APGBN"];
				vo.APGBN_TX		= rec["APGBN_TX"];
				vo.CARDNO		= rec["CARDNO"];
				vo.APNO			= rec["APNO"];
				
				
				//프린트사용(총금액)
				vo.PRINT_BNFAMT = String(NumberUtil.NumberFormat(Math.floor(rec["BNFAMT"] * 10 * 10)))+
							ResourceManager.getInstance().getString('Welfare', "CURRENCY");
				totMoney += vo.BNFAMT;
				
				retArr.addItem(vo);
			}
			trace(totMoney +" : "+vo.BNFAMT);
			return retArr;	
		}
		
		/****
		 * 건별계좌정보 및 처리상태
		 * ***/
		private function parseWelfareDetailMaster(srcArr:ArrayCollection):WelfareDetailVO
		{
			var retVO:WelfareDetailVO = new WelfareDetailVO();
			if(srcArr.length>0) {
				var rec:Object = srcArr.getItemAt(0);
				retVO.BNKSA 		= rec["BNKSA"];
				retVO.BNKTX 		= rec["BNKTX"];
				retVO.BANKL 		= rec["BANKL"];
				retVO.BANKA 		= rec["BANKA"];
				retVO.BANKN 		= rec["BANKN"];
				retVO.EMFTX 		= rec["EMFTX"];
				retVO.BELNR 		= rec["BELNR"];
				retVO.BUDAT 		= DateUtil.stringToDateTime(rec["BUDAT"]);
				retVO.DSTATUS 		= rec["DSTATUS"];
				retVO.DSTATX 		= rec["DSTATX"];
				retVO.BSTATUS 		= rec["BSTATUS"];
				retVO.BSTATX 		= rec["BSTATX"];
				
				retVO.WSTATUS 		= rec["WSTATUS"];
				retVO.WSTATX 		= rec["WSTATX"];
			}
			return retVO;	
		}
		
		/****
		 * 계좌관리 메인데이터 조회
		 * ***/
		private function parseBankAccount(srcArr:ArrayCollection):void
		{
			var retArr:ArrayCollection = new ArrayCollection();
			
			for each(var rec:Object in srcArr) 
			{
				var vo:BankAccountVO = new BankAccountVO();
				vo.BNKSA = rec["BNKSA"];
				vo.STEXT = rec["STEXT"];
				vo.BANKL = rec["BANKL"];
				vo.BANKA = rec["BANKA"];
				vo.BANKN = rec["BANKN"];
				vo.EMFTX = rec["EMFTX"];
				vo.BANKS = rec["BANKS"];
				if(vo.BNKSA=="0") {
					this.defaultBank = vo;
				}
				else {
					this.additionBank = vo;
				}
			} 
		}
		
		/**
		 * 선택적복리후생 초기화면 list ( 상단 예산금액 )
		 * 
		 * @param srcArr
		 * @return vo
		 * */
		private function parseUsedMoney(srcArr:ArrayCollection):UsedAmountVO
		{
			var vo:UsedAmountVO = new UsedAmountVO();
			for each(var rec:Object in srcArr) 
			{
				vo.BEGDA 		= DateUtil.stringToDateTime(rec["BEGDA"]);
				vo.ENDDA 		= DateUtil.stringToDateTime(rec["ENDDA"]);
				vo.CAFBT 		= int(rec["CAFBT"] * 10 * 10);
				vo.USEBT 		= int(rec["USEBT"] * 10 * 10);
				vo.REMBT 		= int(rec["REMBT"] * 10 * 10);
			}
			return vo;	
		}
		
		/**
		 * 개인정보
		 * 
		 * @param
		 * @return 개인정보 Value Object
		 */
		private function parseProfile(retObj:Object):ProfileVO
		{
			var vo:ProfileVO = new ProfileVO();
			vo.PERNR		= retObj["PERNR"];
			vo.ENAME		= retObj["ENAME"];
			vo.PHOTO_URL	= retObj["PHOTO_URL"];
			vo.TEL_HOME		=retObj["TEL_HOME"];			// 집전화
			vo.TEL_OFFICE	=retObj["TEL_OFFICE"];		// 회사전화
			vo.TEL_MOBILE1	=retObj["TEL_MOBILE1"];		// 이동전화 1
			vo.TEL_MOBILE2	=retObj["TEL_MOBILE2"];		// 이동전화 2
			vo.FAX			=retObj["FAX"];				// 팩스
			vo.EMAIL_IN		=retObj["EMAIL_IN"];			// 사내메일	
			vo.EMAIL_EX		=retObj["EMAIL_EX"];			// 사외메일
			vo.ORGEH		=retObj["ORGEH"];			// 조직 코드
			vo.ORGTX		=retObj["ORGTX"];			// 부서명
			vo.PERSK		=retObj["PERSK"];			// 직군코드
			vo.PTEXT		=retObj["PTEXT"];			// 직군
			vo.STELL		=retObj["STELL"];			// 직무 코드
			vo.STELLT		=retObj["STELLT"];			// 직무명
			vo.BAND			=retObj["BAND"];				// 밴드코드
			vo.BANDT		=retObj["BANDT"];			// 밴드명
			vo.IDATE		= DateUtil.stringToDateTime(retObj["IDATE"]);			// 입사일
			vo.ODATE		= DateUtil.stringToDateTime(retObj["ODATE"]);			// 부서배치일
			vo.PDATE		= DateUtil.stringToDateTime(retObj["PDATE"]);			// 승진기준일
			vo.ANKEY		=retObj["ANKEY"];			// 근무지코드
			vo.ANKEYT		=retObj["ANKEYT"];			// 근무지명
			vo.MASSG		=retObj["MASSG"];			// 입사경로 코드
			vo.MASSGT		=retObj["MASSGT"];			// 입사경로명
			return vo;
		}
	}
}